package com.lyp.supermanage.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lyp.supermanage.R;
import com.lyp.supermanage.api.Api;
import com.lyp.supermanage.bean.StatementBean;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends Activity implements View.OnClickListener {

    private LinearLayout mCar,mDection,mIndoor;
    private TextView mStatement,mCarOrder,mDecationOrder,mIndoorOrder,mCarMoney,mDectionMoney;
    private TextView mIndoorMoney,mCarAccumulate,mDectionAccumulate,mIndoorAccumulate;
    private ArrayList<StatementBean.DataBean> mBeen = new ArrayList<>();

    private long currentBackPressedTime = 0;
    private static final int BACK_PRESSED_INTERVAL = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }

    private void initview(){
        mCar  = (LinearLayout) findViewById(R.id.ll_car);
        mDection  = (LinearLayout) findViewById(R.id.ll_decation);
        mIndoor  = (LinearLayout) findViewById(R.id.ll_indoor);
        mStatement  = (TextView) findViewById(R.id.tv_statement);
        mCarOrder  = (TextView) findViewById(R.id.car_order);
        mDecationOrder  = (TextView) findViewById(R.id.decation_order);
        mIndoorOrder  = (TextView) findViewById(R.id.indoor_order);
        mCarMoney  = (TextView) findViewById(R.id.car_money);
        mDectionMoney  = (TextView) findViewById(R.id.decation_money);
        mIndoorMoney  = (TextView) findViewById(R.id.indoor_money);
        mCarAccumulate  = (TextView) findViewById(R.id.car_accumulate);
        mDectionAccumulate  = (TextView) findViewById(R.id.decation_accumulate);
        mIndoorAccumulate  = (TextView) findViewById(R.id.indoor_accumulate);
        mCar.setOnClickListener(this);
        mDection.setOnClickListener(this);
        mIndoor.setOnClickListener(this);
        statement();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()){
            case R.id.ll_car:
                intent = new Intent(this, DetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("databean", mBeen.get(0).getCar());
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.ll_decation:
                intent = new Intent(this, DetailsActivity.class);
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("databean", mBeen.get(0).getDetection());
                intent.putExtras(bundle1);
                startActivity(intent);
                break;
            case R.id.ll_indoor:
                intent = new Intent(this, DetailsActivity.class);
                Bundle bundle3 = new Bundle();
                bundle3.putSerializable("databean", mBeen.get(0).getGovern());
                intent.putExtras(bundle3);
                startActivity(intent);
                break;
        }

    }


    //报表
    public void statement() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Api api = retrofit.create(Api.class);
        Call<StatementBean> call = api.getStatement();
        call.enqueue(new Callback<StatementBean>() {
            @Override
            public void onResponse(Call<StatementBean> call, final Response<StatementBean> response) {
                if (response.body().getResult() == 1 ) {
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBeen.add(response.body().getData());
                            double carPrice = Double.parseDouble(mBeen.get(0).getCar().getDayPrice());
                            double dectionPrice = Double.parseDouble(mBeen.get(0).getDetection().getDayPrice());
                            double indoorPrice = Double.parseDouble(mBeen.get(0).getGovern().getDayPrice());
                            double sum = carPrice+dectionPrice+indoorPrice;
                            mStatement.setText(sum+"");
                            mCarOrder.setText(mBeen.get(0).getCar().getNum());
                            mDecationOrder.setText(mBeen.get(0).getDetection().getNum());
                            mIndoorOrder.setText(mBeen.get(0).getGovern().getNum());
                            mCarMoney.setText(carPrice+"");
                            mDectionMoney.setText(dectionPrice+"");
                            mIndoorMoney.setText(indoorPrice+"");
                            mCarAccumulate.setText(mBeen.get(0).getCar().getMonthPrice());
                            mDectionAccumulate.setText(mBeen.get(0).getDetection().getMonthPrice());
                            mIndoorAccumulate.setText(mBeen.get(0).getGovern().getMonthPrice());
                        }
                    });
                } else{
                    ToastUtils.showShort(response.body().getMessage());
                }
            }
            @Override
            public void onFailure(Call<StatementBean> call, Throwable t) {
                ToastUtils.showShort(t.getMessage());
            }
        });
    }


    //退出程序
    private long exitTime = 0;
    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - exitTime > 2000) {
            ToastUtils.showShort("再按一次退出程序");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}
